/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package run;



import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author luckymancvp
 */
public class ServerPlayer extends Thread{

    public static Socket socket          = null;
    private String playerName      = null;
    private int mark = 0;
    private String answerFromClient = null;
    private int vitri;
    public static final int NAME_TYPE = 1;



    public ServerPlayer(Socket socket){
        vitri = ServerStart.countPlayer+1;
        this.socket = socket;
        this.start();
    }
    public void setVitri(int i){
    this.vitri = i;
    }

    public int getVitri(){
    return vitri;
    }

    public String getAnswer(){
    return answerFromClient;
    }

    public String getPlayerName(){
    return playerName;
    }

   public void xuly2(String msg){
        /* Nhan message tu client
        Neu co dang 1#xxx thi xxx la ten -> setPlayerName = xxx
       Neu khong thi la cau tra loi cua Client gui ve -> luu cau tra loi.
         */

       int type = 0;
       String msg1 = msg;
       this.answerFromClient = msg;
/* check if answer was correct */
        StringTokenizer st = new StringTokenizer(msg,"#");
/* get question number */
        if (st.hasMoreTokens()) {
        try {
           type = Integer.parseInt(st.nextToken()) - 1;

           msg1 = st.nextToken().trim();
            if(type == (NAME_TYPE)-1) {
                this.playerName = msg1;
                System.out.println("Name:"+this.playerName +'\n');
                }
            } catch (Exception e) {
            //return "";
            }
        } else {
             System.out.println("Anwser:"+msg);
        }
               System.out.println("|||"+msg+ " " +vitri);

            //    return "msg1";
       }
   public void displayScreen(){
         if (this.getVitri()==1){
                ServerScreen.setName1(this.getPlayerName());
                ServerScreen.setAnswer1(this.getAnswer());
               // ServerScreen.set
                }
                 else if (this.getVitri()==2){
                ServerScreen.setName2(this.getPlayerName());
                ServerScreen.setAnswer2(this.getAnswer());
                 }
                else if (this.getVitri()==3){
                ServerScreen.setName3(this.getPlayerName());
                ServerScreen.setAnswer3(this.getAnswer());
                 }
                else if (this.getVitri()==4){
                ServerScreen.setName4(this.getPlayerName());
                ServerScreen.setAnswer4(this.getAnswer());

                 }
   }
 @Override
    public void run(){
        while(true) {
            try {
                /// Nhan message client gui
                // Ten| xxxx . Goi toi ham trong MainScreen la setText(messge, vitri) ten cho nguoi choi
                // Ans| xxx   .
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String ans1 = inFromClient.readLine();

                xuly2(ans1);
               // this.displayScreen(this);
                 if (this.getVitri()==1){
                ServerScreen.setName1(this.getPlayerName());
                ServerScreen.setAnswer1(this.getAnswer());
               // ServerScreen.set
                }
                 else if (this.getVitri()==2){
                ServerScreen.setName2(this.getPlayerName());
                ServerScreen.setAnswer2(this.getAnswer());
                 }
                else if (this.getVitri()==3){
                ServerScreen.setName3(this.getPlayerName());
                ServerScreen.setAnswer3(this.getAnswer());
                 }
                else if (this.getVitri()==4){
                ServerScreen.setName4(this.getPlayerName());
                ServerScreen.setAnswer4(this.getAnswer());

                 }

                
               System.out.println("From "+playerName+":"+ans1);
            } catch (IOException ex) {
                Logger.getLogger(ServerPlayer.class.getName()).log(Level.SEVERE, null, ex);
            }


            }
    }

    public void sendMessage(String content) throws IOException{
        DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
        outToClient.writeBytes(content+"\n");
        outToClient.flush();
    }

}
